Ext.define('Gigi.controller.Menus', {
	extend : 'Ext.app.Controller',

	views : ['menu.Tree', 'menu.Edit'],
	stores : ['Menus'],
	models : ['Menu'],

	init : function() {
		this.control({
					'#add' : {
						click : this.onAddClick
					},
					'#delete' : {
						click : this.onDeleteClick
					},
					'#update' : {
						click : this.onUpdateClick
					},
					'menuedit button[action=save]' : {
						click : this.onEditSaveClick
					}
				});
	},

	onAddClick : function(button) {
		var tree = button.up('menutree'), selModel = tree.getSelectionModel();
		var win = Ext.widget('menuedit', {
					title : '新增菜单'
				});
		var form = win.down('form');
		var menu = Ext.create('Gigi.model.Menu');
		// 未选择节点则加入根节点下
		if (selModel.getCount() == 0) {
			menu.set('parent_code', '');
		} else {
			menu.set('parent_code', selModel.getSelection()[0].get('code'));
		}
		form.loadRecord(menu);
	},

	onDeleteClick : function(button) {
		var tree = button.up('menutree'), selModel = tree.getSelectionModel();
		if (selModel.getCount() != 1) {
			Ext.Msg.show({
						title : '删除菜单',
						msg : '请选择一条记录',
						icon : Ext.MessageBox.ERROR,
						buttons : Ext.MessageBox.OK
					});
		} else {
			var selectNode = selModel.getSelection()[0];
			if (selectNode.hasChildNodes()) {
				Ext.Msg.show({
							title : '删除菜单',
							msg : '当前菜单下已经有子菜单，不允许删除！',
							icon : Ext.MessageBox.ERROR,
							buttons : Ext.MessageBox.OK
						});
			} else {
				Ext.Msg.show({
							title : '删除菜单',
							msg : '您确认删除吗？',
							icon : Ext.MessageBox.QUESTION,
							fn : function(btn) {
								if (btn == "ok") {
									selectNode.destroy({
												success : function() {
													Ext.MessageBox.alert(
															'删除菜单', '删除成功！');
													selectNode.remove(true);
												},
												failure : function(response,
														opts) {
													Ext.MessageBox
															.alert(
																	'删除菜单',
																	response.responseText);
												}
											});
								}
							},
							buttons : Ext.MessageBox.OKCANCEL
						});
			}
		}
	},

	onUpdateClick : function(button) {
		var tree = button.up('menutree'), selModel = tree.getSelectionModel();
		if (selModel.getCount() != 1) {
			Ext.Msg.show({
						title : '修改用户组',
						msg : '请选择一条记录',
						icon : Ext.MessageBox.ERROR,
						buttons : Ext.MessageBox.OK
					});
		} else {
			var win = Ext.widget('menuedit', {
						title : '修改用户组',
						actionType : 'update'
					});
			var form = win.down('form');
			var menu = Ext.ModelManager.getModel('Gigi.model.Menu');
			menu.load(selModel.getSelection()[0].get('code'), {
						success : function(menu) {
							form.loadRecord(menu);
						}
					});

		}
	},

	onEditSaveClick : function(button) {
		var win = button.up('window'), form = win.down('form').getForm(), menutree = Ext.ComponentQuery
				.query('menutree')[0], selModel = menutree.getSelectionModel();
		if (form.isValid()) {
			var menu = Ext.create('Gigi.model.Menu', form.getValues());
			var operation = win.actionType;
			if (operation == 'create') {
				menu.phantom = true;
			} else {
				menu.phantom = false;
			}
			menu.save({
				success : function(record, operation) {
					Ext.MessageBox.alert('保存菜单', '保存成功！');
					win.close();
					if (selModel.getCount() == 0) {
						menutree.getStore().load();
					} else {
						if (operation == 'create') {
							menutree.getStore().load({
										node : selModel.getSelection()[0]
									});
						} else {
							menutree.getStore().load({
										node : selModel.getSelection()[0].parentNode
									});
						}
					}
				},
				failure : function(record, operation) {
					Ext.MessageBox.alert('保存菜单', '保存菜单失败！');
				}
			});
		};
	}
});